var express = require('express');
var config = require('./config/index');
var axios = require('axios');

var port = process.env.PORT || config.build.port;

var app = express();

var apiRoutes = express.Router();

// apiRoutes.get('/getDiscList', function (req, res) {
//   var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg';
//   axios.get(url, {
//     headers: {
//       referer: 'https://c.y.qq.com/',
//       host: 'c.y.qq.com'
//     },
//     params: req.query
//   }).then((response) => {
//     res.json(response.data)
//   }).catch((e) => {
//     console.log(e)
//   })
// });
//
// apiRoutes.get('/lyric', function (req, res) {
//   var url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg';
//
//   axios.get(url, {
//     headers: {
//       referer: 'https://c.y.qq.com/',
//       host: 'c.y.qq.com'
//     },
//     params: req.query
//   }).then((response) => {
//     var ret = response.data;
//     if (typeof ret === 'string') {
//       var reg = /^\w+\(({[^\(\)]+})\)$/;
//       var matches = response.data.match(reg);
//       if (matches) {
//         ret = JSON.parse(matches[1])
//       }
//     }
//     res.json(ret)
//   }).catch((e) => {
//     console.log(e)
//   })
// });


app.get('/api/getDiscList',(req,res) => {
  const url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg';
  axios.get(url,{
    headers:{ // 在headers里面配置相关代理接口的参数
      /*
      * *欺骗浏览器说你的host以及referer信息如下。跨域问题至此已不存在，
      * *同时解决host以及referer的限制问题。
      * **/
      referer: 'https://c.y.qq.com',
      host: 'c.y.qq.com'
    },
    params:req.query
  }).then((response) => {
    res.json(response.data)
  }).catch((error) => {
    console.log(error)
  })
});

// 请求歌曲资源
app.get('/api/music',function (req,res) {
  const url = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg';
  axios.get(url,{
    headers: {
      referer: 'https//c.y.qq.com/',
      host: 'c.y.qq.com'
    },
    params: req.query
  }).then( response => {
    res.json(response.data)
  }).catch( error => {
    console.log(error)
  })
});
// 请求歌词
app.get('/api/lyric',function (req,res) {
  const url = 'https://szc.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg';
  // const url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg';
  axios.get(url,{
    headers: {
      referer: 'https://szc.y.qq.com/',
      host: 'szc.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    // 正则匹配jsonp数据，处理截取出来后返回
    var ret = response.data;
    if (typeof ret === 'string') {
      var reg = /^\w+\(({[^()]+})\)$/;
      var matches = ret.match(reg);
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret)
  }).catch((e) => {
    console.log(e)
  })
});
// 获取歌单详情
app.get('/api/getSonglist',function (req, res) {
  const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg';
  axios.get(url,{
    headers: {
      referer: 'https://y.qq.com/',
      host: 'c.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    // 对得到的数据进行处理
    // 正则匹配jsonp数据，处理截取出来后返回
    var ret = response.data;
    if (typeof ret === 'string') {
      var reg = /^\w+\(({[^()]+})\)$/;
      var matches = ret.match(reg);
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret);
  }).catch((e) => {
    console.log(e)
  })
});
//  搜索歌曲歌手
app.get('/api/search',function(req,res) {
  const url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp';
  axios.get(url,{
    headers:{
      referer: 'https://c.y.qq.com/',
      host: 'c.y.qq.com'
    },
    params: req.query
  }).then((response) => {
    // 对得到的数据进行处理
    // 正则匹配jsonp数据，处理截取出来后返回
    var ret = response.data;
    if (typeof ret === 'string') {
      var reg = /^\w+\(({[^()]+})\)$/;
      var matches = ret.match(reg);
      if (matches) {
        ret = JSON.parse(matches[1])
      }
    }
    res.json(ret);
  }).catch((e) => {
    console.log(e)
  })
});


app.use('/api', apiRoutes);

app.use(express.static('./dist'));

module.exports = app.listen(port, function (err) {
  if (err) {
    console.log(err);
    return
  }
  console.log('Listening at http://localhost:' + port + '\n')
});
